package com.walker.order.controller;


import java.io.UnsupportedEncodingException;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.messaging.Message;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.walker.order.client.ProductClinet;
import com.walker.order.config.User;
import com.walker.order.message.SenderService;

@RestController
@RequestMapping(value = "/order")
public class OrderController {
	
/*	@Autowired
	private LoadBalancerClient loadBalancerClient;*/
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private ProductClinet productClinet;
	
	@Autowired
	private User user;
	
	@Autowired
	private SenderService senderService;
	
	@Autowired
	private AmqpTemplate amqpTemplate;

	@RequestMapping(value = "/productList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public String getProductList() {
		/**
		 *  调用方式一
		RestTemplate restTemplate = new RestTemplate();
		String productList = restTemplate.getForObject("http://localhost:8090/productList", String.class);
		*/
		
		/**
		 *  调用方式二
		RestTemplate restTemplate = new RestTemplate();
		ServiceInstance serviceInstance = loadBalancerClient.choose("product");
		String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort());
		String productList = restTemplate.getForObject(url + "/productList", String.class);
		System.out.println("====================================================" + url);
		 */
		
		/**
		 * 调用方式三
		String productList = restTemplate.getForObject("http://product/productList", String.class);
		 */
		String productList = productClinet.findProductList();
		return "order invoke droduct by feign:" + productList;
	}
	
	@RequestMapping(value = "/configRefresh", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Object getConfig() {
		User user = new User();
		user.setName(this.user.getName());
		user.setAge(this.user.getAge());
		return user;
	}
	
	@RequestMapping(value = "/sendMessage/{message}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public String sendMessage(@PathVariable("message") String message) throws UnsupportedEncodingException {
		Message<byte[]> msg = MessageBuilder.withPayload(message.getBytes()).build();
		SubscribableChannel channel = senderService.sendMessage();
		channel.send(msg);
		return "success";
	}
	
	@RequestMapping(value = "/sendMessageMQ/{message}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public String sendMessageMQ(@PathVariable("message") String message) throws UnsupportedEncodingException {
//		amqpTemplate.convertAndSend("rabbit-message",message);
		amqpTemplate.convertAndSend("product","product-msg",message);
		return "success" + message;
	}
}
